/**
 * memcached 缓存类
 */

const Memcached = require('memcached');
const memcached = new Memcached('127.0.0.1:11211');

/**
 * 获取缓存
 * @param {Object} key
 */
function get(key, callback){
  memcached.get(key,(err,row)=>{
    if(err){
      console.log(err);
    }else {
      if(typeof(row) == 'string'){
        row = JSON.parse(row);
      }
      callback(err, row);
      //memcached.end();
    }
  });
}

/**
 * 设置缓存
 * @param {Object} key
 * @param {Object} data
 * @param {Object} timeout
 */
function set(key, data, expire){
  memcached.set(key, JSON.stringify(data), expire, function (err) {
    console.log('ok');
  });
}


function flush(key){
  delete global.cacheData;
}

exports.get = get;
exports.set = set;
exports.flush = flush;